package com.zhku.meeting.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.MeetingRecord;
import com.zhku.meeting.entities.MeetingRecordVo;
import com.zhku.meeting.entities.bo.PredicateMsgBo;
import com.zhku.meeting.entities.bo.QueryMsgBo;
import com.zhku.meeting.enums.CodeEnum;
import com.zhku.meeting.mapper.MeetingRecordMapper;
import com.zhku.meeting.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MeetingRecordService extends ServiceImpl<MeetingRecordMapper, MeetingRecord> {

    @Autowired
    private MeetingRecordMapper meetingRecordMapper;

    public ResultVO<?> getHistory(Map<String, Object> para) {
        Integer pass = Integer.parseInt((String) para.get("pass"));
        long a = System.currentTimeMillis();
        Date meetingDate = new Date(a);
        Integer deptId = null;
        if (para.containsKey("deptId")) {    //判断是否管理员
            deptId = Integer.parseInt((String) para.get("deptId"));
        }
        Integer roomId = null;
        if (para.containsKey("roomId")) {
            roomId = Integer.parseInt((String) para.get("roomId"));
        }
        Integer meetSlot = null;
        if (para.containsKey("meetSlot")) {
            meetSlot = Integer.parseInt((String) para.get("meetSlot"));
        }
        Integer pageSize = Integer.parseInt((String) para.get("pageSize"));
        Integer currentPage = Integer.parseInt((String) para.get("currentPage"));
        MeetingRecordVo meetingRecord = new MeetingRecordVo();
        meetingRecord.setDeptId(deptId);
        meetingRecord.setMeetingDate(meetingDate);
        meetingRecord.setRoomId(roomId);
        meetingRecord.setMeetingSlot(meetSlot);
        PageHelper.startPage(currentPage, pageSize);
        List<MeetingRecordVo> meetingRecords = meetingRecordMapper.selectByCondition(meetingRecord, pass);
        PageInfo pageInfo = new PageInfo(meetingRecords);
        Map<String, Object> map = new HashMap<>();
        map.put("result", pageInfo);
        return ResultVO.successData(map);
    }

    public void addRecord(MeetingRecord meetingRecord) {
        meetingRecordMapper.insert(meetingRecord);
    }

    public void revoke(Integer applyId) {
        meetingRecordMapper.deleteByPrimaryKey(applyId);
    }

    public ResultVO<?> predicateMsg(PredicateMsgBo predicateMsgBo) {
        LambdaUpdateWrapper<MeetingRecord> meetingRecordLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        meetingRecordLambdaUpdateWrapper.eq(MeetingRecord::getRecordId, predicateMsgBo.getRecordId());
        meetingRecordLambdaUpdateWrapper.eq(!(StringUtils.isEmpty(predicateMsgBo.getDeptId())), MeetingRecord::getDeptId, predicateMsgBo.getDeptId());
        meetingRecordLambdaUpdateWrapper.lt(MeetingRecord::getMeetingDate, LocalDateTime.now());
        MeetingRecord meetingRecord1 = new MeetingRecord();
        meetingRecord1.setPredicateMsg(predicateMsgBo.getPredicateMsg());
        meetingRecord1.setEvaluationLevel(predicateMsgBo.getEvaluationLevel());
        int update = meetingRecordMapper.update(meetingRecord1, meetingRecordLambdaUpdateWrapper);
        if (update > 0)
            return ResultVO.success();
        return ResultVO.errorData("评价失败,会议不存在/暂未结束", CodeEnum.CODE_1012);
    }

    public ResultVO<?> getMsg(QueryMsgBo queryMsgBo) {
        LambdaQueryWrapper<MeetingRecord> meetingRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        meetingRecordLambdaQueryWrapper.eq(MeetingRecord::getRecordId, queryMsgBo.getRecordId());
        meetingRecordLambdaQueryWrapper.eq(MeetingRecord::getDeptId, queryMsgBo.getDeptId());
        MeetingRecord meetingRecord = meetingRecordMapper.selectOne(meetingRecordLambdaQueryWrapper);
        return ResultVO.successData(meetingRecord);
    }
//    @Autowired
//    private MeetingRecordDao meetingRecordDao;
//    public MeetingRecord getRecord(Integer id){
//        return meetingRecordDao.findById(id).get();
//    }
}
